<?php
/**
 * Yii view _form
 *
 * @author nguyenmanhluu
 * @since Aug 8, 2013 - 7:43:59 PM
 */
?>
<div class="box span12">
    <div class="box-header">
        <h2>
            <?php if ($model->isNewRecord): ?>
                <i class="icon-plus"></i>
                Add
            <?php else: ?>
                <i class="icon-edit"></i>
                Edit
            <?php endif; ?>
            <?php echo $this->title ?>
        </h2>
        <div class="box-icon">
            <a href="#" onclick="return closeForm()">
                <i class="icon-remove"></i>
            </a>
        </div>
    </div>
    <div class="box-content">
        <?php
        $form_id = 'form';
        $form_id .= $model->isNewRecord ? '-add' : '-edit';
        $action  = '';

        if (isset($model->createUrl) && isset($model->editUrl))
        {
            $action = ($model->isNewRecord) ? $model->createUrl : $model->editUrl;
        }

        $formOptions = array(
            'action'                 => $action,
            'id'                     => $form_id,
            'enableClientValidation' => true,
//            'enableAjaxValidation' => true,
            'htmlOptions'            => array(
                'class'   => 'form-horizontal',
                'enctype' => 'multipart/form-data',
            ),
            'clientOptions'          => array(
                'validateOnSubmit' => true,
            ),
        );

        $form = $this->beginWidget('CActiveForm', $formOptions);
        ?>
        <?php echo CHtml::errorSummary($model); ?>
        <fieldset>
            <?php foreach ($this->formColumns as $column => $opts): ?>

                <div class="control-group">
                    <?php echo $form->labelEx($model, $column, array('class' => 'control-label')); ?>
                    <div class ="controls">
                        <?php
                        $type = isset($opts['type']) ? $opts['type'] : '';
                        switch ($type)
                        {
                            case 'textArea':
                                echo $form->textArea($model, $column, array(
                                    'style'       => 'height:70px',
                                    'class'       => 'span8',
                                    'placeholder' => $model->getAttributeLabel($column),
                                ));
                                break;

                            case 'editor':
                                echo $form->textArea($model, $column, array(
                                    'style'       => 'height:70px;',
                                    'class'       => 'cleditor',
                                    'placeholder' => $model->getAttributeLabel($column),
                                ));
                                break;

                            case 'wideEditor':
                                echo $form->textArea($model, $column, array(
                                    'style'       => 'height:70px;',
                                    'class'       => 'wideCleditor',
                                    'placeholder' => $model->getAttributeLabel($column),
                                ));
                                break;

                            case 'radioButtonList':
                                $data = isset($opts['data']) ? $opts['data'] : array();
                                echo $form->radioButtonList($model, $column, $data, array(
                                    'template' => '<label class="radio">{input} {label}</label>'
                                ));
                                break;

                            case 'dropDownList':
                                $data = isset($opts['data']) ? $opts['data'] : array();
                                echo $form->dropDownList($model, $column, $data);
                                break;
//
//                            case 'dropDownListChosen':
//                                $data = isset($opts['data']) ? $opts['data'] : array();
//                                $select = isset($opts['selected']) ? $opts['selected'] : array();
//                                echo CHtml::dropDownList($column . '[]', $select,$data, array(
//                                    'multiple data-rel' => "chosen",
//                                    'data-placeholder' => "Select some service..."
//                                ));
//                                break;
                            case 'inputPrepend':
                                echo '<div class="input-prepend">';
                                echo '<span class="add-on">' . $opts['pendValue'] . '</span>';
                                echo $form->textField($model, $column, array(
                                    'placeholder' => $model->getAttributeLabel($column),
                                    'class'       => 'input-file uniform_on input-reset span6'
                                ));
                                echo '</div>';
                                break;
                            case 'inputAppend':
                                echo '<div class="input-append">';
                                echo $form->textField($model, $column, array(
                                    'placeholder' => $model->getAttributeLabel($column),
                                    'class'       => 'input-file uniform_on input-reset span6'
                                ));
                                echo '<span class="add-on">' . $opts['pendValue'] . '</span>';
                                echo '</div>';
                                break;
                            default:
                                echo $form->textField($model, $column, array(
                                    'placeholder' => $model->getAttributeLabel($column),
                                    'class'       => 'input-file uniform_on input-reset span6'
                                ));
                                break;
                        }
                        ?>
                        <font color="red"><?php echo $form->error($model, $column); ?></font>
                    </div>
                </div>

            <?php endforeach; ?>           
            <div class="control-group">       
                <div class ="controls">
                    <?php
                    if ($this->ajaxControl)
                    {
                        $url = '';
                        if (isset($model->listUrl))
                        {
                            $url = $model->listUrl;
                        }

                        echo CHtml::ajaxSubmitButton("Save $this->title", $action, array(
                            'success' => "function(data) {formSuccess(data, '$url');}",
                                ), array(
                            'id'    => 'submitItem' . $model->id,
                            'name'  => 'submitItem' . $model->id,
                            'class' => 'btn btn-primary'
                        ));
                    }
                    else
                    {
                        echo CHtml::button('Save', array(
                            'class' => 'btn btn-primary',
                            'type'  => 'submit',
                        ));
                    }
                    ?>
                    <input class="btn btn-inverse" onclick="return closeForm();" 
                           type="reset" value="Close" />
                </div>
            </div>
        </fieldset>
        <?php $this->endWidget(); ?>
    </div>
</div>
<script>
    $('.wideCleditor').cleditor({
        width: 800 // width not including margins, borders or padding
    });
</script>